# (1)	题目描述：
# 通过OpenCV读取一张图片，完成下面的操作：1、滤波 2、二值化 3、霍夫直线检测 4、仿射变换
# (2)	题目要求：.
# ①　导入相关头文件
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from python_ai.common.xcommon import *

spr = 2
spc = 4
spn = 0
plt.figure(figsize=[12, 6])


def my_show_pic(img, title):
    global spn
    spn += 1
    plt.subplot(spr, spc, spn)
    plt.axis('off')
    plt.imshow(img, cmap='gray')
    plt.title(title)


# ②　读入一张图片并转为灰度图
img_path = '../../../../large_data/pic/sudoku.png'
img = cv.imread(img_path, cv.IMREAD_GRAYSCALE)
my_show_pic(img, 'ori')

# ③　完成滤波操作
hpf = cv.Laplacian(img, -1, ksize=5)
my_show_pic(hpf, 'Laplacian -1')
print_numpy_ndarray_info(hpf, 'Laplacian -1')

hpf = cv.Laplacian(img, cv.CV_64F, ksize=5)
my_show_pic(hpf, 'Laplacian cv.CV_64F')
print_numpy_ndarray_info(hpf, 'Laplacian cv.CV_64F')

hpf = cv.Laplacian(img, cv.CV_8U, ksize=5)
my_show_pic(hpf, 'Laplacian cv.CV_8U')
print_numpy_ndarray_info(hpf, 'Laplacian cv.CV_8U')

# hpf = cv.Laplacian(img, np.uint8, ksize=5)  # Argument 'ddepth' is required to be an integer
# my_show_pic(hpf, 'Laplacian np.uint8')
# print_numpy_ndarray_info(hpf, 'Laplacian np.uint8')

# ④　将图像转变为二值图
bin = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2)
my_show_pic(bin, 'Adaptive binary')

# ⑤　完成标准霍夫直线检测
# ⑥　画出霍夫直线检测的结果
# ⑦　完成概率霍夫直线检测
# ⑧　画出概率霍夫直线检测结果
# ⑨　完成霍夫圆检测
# ⑩　画出霍夫圆检测的结果

# 11　完成仿射变换，放大2倍，旋转45度
H, W = img.shape
M = cv.getRotationMatrix2D((H // 2, W // 2), 45, 2)
affine = cv.warpAffine(img, M, (W, H))

# 12　展示仿射变换的结果
my_show_pic(affine, 'Affine')

# 13　加入必要注释
